package com.heima.shop.service.impl;

import com.heima.shop.mapper.StoreUserMapper;
import com.heima.shop.service.StoreUserService;
import com.heima.common.exception.CustomException;
import com.heima.model.shop.dtos.StoreUserDto;
import com.heima.model.shop.pojos.StoreUser;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser> implements StoreUserService {

    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(StoreUserDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        dto.checkParam();

        //2 执行查询
        // 分页
        Page page = new Page(dto.getPage(), dto.getSize());
        // 条件查询
        LambdaQueryWrapper<StoreUser> wraaper = new LambdaQueryWrapper<>();

        // 用户名
         if (StringUtils.isNotBlank(dto.getUsername())) {
            wraaper.like(StoreUser::getUsername, dto.getUsername());
         }
        // 密码
         if (StringUtils.isNotBlank(dto.getPassword())) {
            wraaper.like(StoreUser::getPassword, dto.getPassword());
         }
        // 姓名
         if (StringUtils.isNotBlank(dto.getRealname())) {
            wraaper.like(StoreUser::getRealname, dto.getRealname());
         }
        // 手机
         if (StringUtils.isNotBlank(dto.getPhone())) {
            wraaper.like(StoreUser::getPhone, dto.getPhone());
         }
        // 是否是管理员
         if (StringUtils.isNotBlank(dto.getIsAdmin())) {
            wraaper.like(StoreUser::getIsAdmin, dto.getIsAdmin());
         }



        IPage pageResult = page(page, wraaper);

        //3 封装分页结果
        return PageResponseResult.page(dto.getPage(), dto.getSize(),
                (int) pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param storeUser
     * @return
     */
    @Override
    public ResponseResult insert(StoreUser storeUser) {
        //1 参数检查
        if (storeUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行保存
        save(storeUser);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(StoreUser storeUser) {

        //1.检查参数
        if(null == storeUser){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(storeUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
